package com.seadragon.app.ksr.controller;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.seadragon.app.ksr.model.Customer;
import com.seadragon.app.ksr.model.Office;
import com.seadragon.app.ksr.service.CustomerService;
import com.seadragon.app.ksr.service.OfficeService;
import com.seadragon.app.ksr.service.ReviewService;
import com.seadragon.app.ksr.util.CacheManager;
import com.seadragon.app.ksr.util.CookieUtil;

@Controller
public class IndexController {
	@Autowired
	private CustomerService customerService;

	@Autowired
	private OfficeService officeService;

	@Autowired
	private CacheManager cacheManager;
	
	@Autowired
	private ReviewService reviewService;	
	@ModelAttribute
	public void populateModel(Model model) {
		model.addAttribute("categoryList", cacheManager.getAllLeafCatogories());
	}	
	
   @RequestMapping("/index.html")
	public String show(HttpServletRequest request, Model model){
	   //show first 2
	   model.addAttribute("interestedOffices", DummyData.getOfficeList().subList(0, 2));
	   List<Map<String,Object>> entityList = new ArrayList<Map<String,Object>>();
	   Map<String,Object> entityMap = null;
	   try {
		JsonArray idList = CookieUtil.getIdListFromCookie(request.getCookies());
		JsonObject jo= null;
		Office office = null;
		Customer reviewer = null;
		if (idList!=null){
			for (JsonElement el : idList) {
				if (el instanceof JsonObject){
					jo = (JsonObject)el;
					entityMap=new HashMap<String, Object>();
					if (jo!=null&&"office".equals(jo.get("type").getAsString())){
						office = officeService.findById(new Long(jo.get("id").getAsString()));
						if(office != null)
							entityMap.put("office"+office.getId(), office);
						entityList.add(entityMap);
					} else if (jo!=null&&"reviewer".equals(jo.get("type").getAsString())){
						reviewer = customerService.findById((new Long(jo.get("id").getAsString())));
						entityMap.put("reviewer"+reviewer.getId(), reviewer);
						entityList.add(entityMap);
					}
				}
			}
			model.addAttribute("recentlyViewedList", entityList);
		}
	} catch (UnsupportedEncodingException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
		return "customer.home";
	}
   @RequestMapping("/contact-us.html")
   public String showContactUs(Model model){
	   return "customer.contactUs";
   }
   @RequestMapping("/business/index.html")
   public String showBusinessHome(Model model){
	   return "business.home";
   }
}

